<?php
class Treatment extends DB
{
    var $_table = 'treatment_subscriber';

    function getList()
    {
	$this->_query = sprintf
	(
	    'SELECT `%1$s`.*,`customer`.`customer_name`,`customer`.`id` AS `customer_id`,'
		.'(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`%1$s`.`creat_manager_id`) AS `c_manager`,'
		.'(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`%1$s`.`responsible_manager_id`) AS `r_manager`,'
		.'(SELECT TIMESTAMPDIFF(MINUTE,NOW(),`task_time`)) AS `before`'
		.' FROM `%1$s`'
		.' JOIN `customer` ON `customer`.`account_id`=`%1$s`.`account_id`'
		.' %2$s %3$s %4$s',
	    $this->_table,
	    $this->_where($this->_filter,'AND',false),
	    $this->_order(),
	    $this->_limit($this->_limits)
	);

	return $this->_getDataArray();
    }

    function getById($id)
    {
	$this->_filter[] = array('`'.$this->_table.'`.`id`',$id);
	$item = $this->getList($this->_filter,false,false);
	$this->_item = $item[0];

	return $this->_item;
    }

    function addFilter($filter)
    {
	if ( is_array($filter) )
	{
	    foreach ( $filter as $key => $value )
	    {
		if ( $key == 'cm_id' )
		{
		    $this->_filter[] = array('`creat_manager_id`',$value);
		    continue;
		}

		if ( $key == 'rm_id' )
		{
		    $this->_filter[] = array('`responsible_manager_id`',$value);
		    continue;
		}

		if ( $key == 'task_startDate' )
		{
		    if ( !empty($filter['task_stopDate']) )
		    {
			$this->_filter[] = sprintf('`date_time` BETWEEN \'%s\' AND \'%s 23:59:59\'',mysql_escape_string($value),mysql_escape_string($filter['task_stopDate']));
		    }
		    else
		    {
			$this->_filter[] = array('`date_time`','>',mysql_escape_string($value.' 23:59:59'));
		    }
		    continue;
		}

		if ( $key == 'task_stopDate' )
		{
		    if ( empty($filter['task_startDate']) )
		    {
			$this->_filter[] = array('`date_time`','<',mysql_escape_string($value));
		    }
		    continue;
		}

		if ( $key == 'start_startDate' )
		{
		    if ( !empty($filter['start_stopDate']) )
		    {
			$this->_filter[] = sprintf('`task_time` BETWEEN \'%s\' AND \'%s 23:59:59\'',mysql_escape_string($value),mysql_escape_string($filter['start_stopDate']));
		    }
		    else
		    {
			$this->_filter[] = array('`task_time`','>',mysql_escape_string($value));
		    }
		    continue;
		}

		if ( $key == 'start_stopDate' )
		{
		    if ( empty($filter['start_startDate']) )
		    {
			$this->_filter[] = array('`task_time`','<',mysql_escape_string($value.' 23:59:59'));
		    }
		    continue;
		}

		if ( $key == 'display' )
		{
		    if ( !empty($value) )
		    {
			$this->_filter[] = array('`close`',$value=='closed'?1:0);
		    }
		    continue;
		}

		if ( $key == 'customer_name' )
		{
		    $this->_filter[] = array('`customer_name`','LIKE',sprintf('%%%s%%',$value));
		    continue;
		}
	    }
	}
    }
}

class Task extends Treatment
{
    function __construct()
    {
	parent::__construct();
	$this->_filter[] = array('`this_is_task`',1);
    }

    function comments($id)
    {
	$this->_query = sprintf
	(
	    'SELECT `task_comment`.*,'
	    .'(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`task_comment`.`manager_id`) AS `manager_name`'
	    .' FROM `task_comment`'
	    .' WHERE `treatment_subscriber_id`=%d'
	    .' ORDER BY `comment_date` ASC',
	    $id
	);

	return $this->_getDataArray();
    }
}

class webTask extends WWW
{
    var $_task;

    function __construct()
    {
	parent::__construct();
	$this->_task = new Task;
	$this->_task->setLimit(500);
	$this->_task->setOrder('task_time',true);
    }

    function getList()
    {
	$this->_smarty->assign('tasks',$this->_task->getList());
	$this->_template = 'admin/task-list.html';
	return $this;
    }

    function getById($id)
    {
	$this->_smarty->assign('task',$this->_task->getById($id));
	$this->_smarty->assign('comments',$this->_task->comments($id));
	$this->_template = 'admin/task.html';
	return $this;
    }

    function addFilter($filter)
    {
	$this->_task->addFilter($filter);
    }
}
?>
